package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品业务层
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setMealDishMapper;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 启用禁用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void StartOrStop(Integer status, Long id) {
        Dish dishStart = Dish.builder().status(status).id(id).build();
        dishMapper.update(dishStart);

    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //判断当前菜品是否存在起售中
        for (Long id : ids) {
            Dish dish = dishMapper.countById(id);
            Integer status = dish.getStatus();
            if (status == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否被套餐关联
        List<Long> setMealId = setMealDishMapper.getSetMealByDishId(ids);
        if (setMealId != null && setMealId.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //可以删除，删除菜品数据
        for (Long id : ids) {
            dishMapper.deleteDish(id);

            //删除菜品关联口味数据
            dishFlavorMapper.deleteFlavor(id);
        }

    }


    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO searchDishById(Long id) {
        Dish dish = dishMapper.countById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectFlavorById(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;


    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //新增一条数据
        dishMapper.insert(dish);

        Long id = dish.getId();
        //新增n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishflavor -> {
            dishflavor.setDishId(id);
        });
        if (flavors != null && flavors.size() > 0) {
            dishFlavorMapper.insertFlavor(flavors);
        }

    }

    /**
     * 根据分类id查询菜品
     *
     * @param name
     * @param categoryId
     * @return
     */
    public List<Dish> searchDishByCategoryId(String name, Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        dish.setName(name);
        List<Dish> dishes = dishMapper.searchDishByCategoryId(categoryId);
        return dishes;

    }

    /**
     * 修改分类
     *
     * @param dishDTO
     */
    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.deleteFlavor(dishDTO.getId());
        dishFlavorMapper.insertFlavor(dishDTO.getFlavors());
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectFlavorById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;

    }
}

